<?php
namespace app\manage\controller;

use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use app\manage\model\User as Usermodel;


class User extends BaseController
{
    public function index()
    {
        $user=new Usermodel();
        $key=input('key','');
        View::assign('key', $key);
        $order='id desc';
        $user=$user
            ->where('name|phone', 'like', '%'.$key.'%')
            ->order($order)->paginate(10, false, ['query'=>request()->param()])->each(function ($item, $key) {
                $shang= Db::name('user')->where('id', $item->fid)->find();
                $item->fname = isset($shang) ? ($shang['name'] ?: $shang['phone']) : '';
                $item->xia = Db::name('user')->where('fid', $item->id)->count('id');
            });
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return View::fetch();
    }
	
    public function add()
    {
        if (request()->isPost()) {
            $data=input('post.');
            if (!($data['f_phone'] and preg_match("/^1[3456789]{1}\d{9}$/", $data['f_phone']))) {
                $this->error('上一级手机号不正确');
            }
            if (!($data['phone'] and preg_match("/^1[3456789]{1}\d{9}$/", $data['phone']))) {
                $this->error('填写手机号不正确');
            }
            if (!$f_user=Db::name('user')->where('phone', $data['f_phone'])->find()) {
                $this->error('找不到上一级信息');
            }
            if (Db::name('user')->where('phone', $data['phone'])->find()) {
                $this->error('手机号已经存在不能重复添加');
            }
            $msg=123456;
            $info=[
                'phone'=>$data['phone'],
                'yzm'=>$msg,
                'fid'=>$f_user['id'],
                'password'=>md5(sha1($msg))
            ];
            $u=Usermodel::create($info);
            Db::name('user')->where('id', $u->id)->update(['user_no'=>$this->sui($u->id)]);
            $this->success('添加成功');
        }
        return View::fetch();
    }
    public function ajax()
    {
        $data=input('param.');
        $user=new Usermodel();
        if ($data['type']=='user_start') {
            if (Usermodel::where('id', $data['id'])->update(['isopen'=> 1])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type']=='user_stop') {
            if (Usermodel::where('id', $data['id'])->update(['isopen'=> 0])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type']=='user_fid') {
            if ($wo=Usermodel::where('fid', $data['id'])->select()->toArray()) {
                return json($wo);
            } else {
                return json([]);
            }
        }
        if ($data['type']=='user_xuan') {
            if ($data['zi']==$data['id']) {
                return json(['code'=>0,'msg'=>'自己不能为自己下一级']);
            }
                        
            if (Usermodel::where('id', $data['zi'])->update(['fid'=>$data['id']])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        return json(['code'=>0,'msg'=>'失败']);
    }
    public function lit()
    {
        $user=new Usermodel();
        $key=input('key','');
        View::assign('key', $key);
        $user=$user->where('fid', input('id'))
            ->where('nickName', 'like', '%'.$key.'%')
            ->order('id desc')->paginate(10, false, ['query'=>request()->param()]);
        View::assign('user', $user);
        $count1=Db::name('user')->where('fid', input('id'))->count();
        View::assign('count1', $count1);
        return View::fetch();
    }
    
    public function xiu()
    {
        if (request()->isPost()) {
            $data=input('post.');
            if ($wo=Db::name('user')->where('id', $data['id'])->find()) {
                if (!$wo['user_no']) {
                    Db::name('user')->where('id', $wo['id'])->update(['user_no'=>$this->sui($wo['id'])]);
                }
                Db::name('user')->where('id', $wo['id'])->update(['password'=>md5(sha1($data['password']))]);
                $this->success('修改成功', url('user/index', ['st'=>1]));
            } else {
                $this->error('修改失败了');
            }
        }
        return View::fetch('', [
                'data'=>Db::name('user')->find(input('id'))
            ]);
    }
    private function sui($id=0)
    {
        $str=[
                0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
            ];
        $sui='';
        for ($i=0;$i<5;$i++) {
            $sui.=$str[mt_rand(0, 35)];
        }
        
        return $sui.$id;
    }
    public function lit2()
    {
        $user=Db::name('user')->where('fid', input('id'))->order('id desc')->select();
        View::assign('user', $user);
        return View::fetch();
    }
}
